<template>
  <div></div>
</template>

<script>
import { mapActions } from "vuex";
import { sso } from "@/api";

export default {
  name: "TokenPage",
  data() {
    return {};
  },
  methods: {
    ...mapActions("user", ["setUsername", "setAvatar", "setUserId"]),
    onError() {
      this.$router.push("/error");
    },
    async setToken() {
      const { taskId, courseId, provinceId, userName, phoneNum, flag } =
        this.$route.query;
      if (!courseId || !taskId) return this.onError();

      localStorage.setItem("examType", JSON.stringify(flag));
      const postData = { taskId, courseId, provinceId, userName, phoneNum };

      sso(postData)
        .then((res) => {
          const userId = res.uuid;
          const userInfo = {
            userId,
          };
          sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
          localStorage.setItem("userInfo", JSON.stringify(userInfo));
          this.setUserId(userId);
          this.$router.push("/exam");
        })
        .catch(() => this.onError());
    },
  },
  created() {
    this.setToken();
  },
};
</script>

<style lang="scss" scoped></style>
